package com.mkj.basic;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @Author Arrebol
 * @Date 2024/8/6 18:10
 * @Project graphical programming
 * @Description：
 * 确认对话框演示
 */
public class ConfirmDialogTest {

    private final JFrame frame = new JFrame("确认对话框");
    private TextArea textArea = new TextArea(6, 30);
    JButton button = new JButton(new AbstractAction("弹出确认对话框") {
        @Override
        public void actionPerformed(ActionEvent e) {
            //弹出确认对话框
            String text = textArea.getText();
            //int result = JOptionPane.showConfirmDialog(frame, text, "确认对话框", JOptionPane.DEFAULT_OPTION);
            //int result = JOptionPane.showConfirmDialog(frame, text, "确认对话框", JOptionPane.YES_NO_OPTION);
            //int result = JOptionPane.showConfirmDialog(frame, text, "确认对话框", JOptionPane.YES_NO_CANCEL_OPTION);
            int result = JOptionPane.showConfirmDialog(frame, text, "确认对话框", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                textArea.append("用户点击了‘是’选项\n");
            } else if (result == JOptionPane.NO_OPTION) {
                textArea.append("用户点击了‘否’选项\n");
            } else if (result == JOptionPane.CANCEL_OPTION) {
                textArea.append("用户点击了‘取消’选项\n");
            }
        }
    });


    public void init() {
        //组装视图
        frame.add(textArea);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new ConfirmDialogTest().init();
    }
}
